package logic.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import logic.util.Util;

/**
 * 多条资讯类型json格式.
 * @author	zouqp
 * @date	2015-1-27
 */

public class MultiNews implements Serializable {
	private static final long serialVersionUID = -5008994260718554756L;
	private int layoutType;//布局类型
	/**
	 * 详情id联合详细类型为键（详细类型  imagelist=图集
	 *  video=视频 audio=音频 book=书籍 imagetext=图文 url=链接），
	 * 对应的单条记录为值
	 * eg:Map("12312321_image","{\"modelContentDetailId\":12312321}"
	 */
	private Map<String,String> dataMap ;
	
	private List<Object>   newss;
	private List<String>   types;
	
	public int getLayoutType() {
		return layoutType;
	}
	public void setLayoutType(int layoutType) {
		this.layoutType = layoutType;
	}
	public Map<String, String> getDataMap() {
		return dataMap;
	}
	public void setDataMap(Map<String, String> dataMap) {
		this.dataMap = dataMap;
	}

	public int getNewsCount( ){
		if(newss == null ) {
			initNewsObjext( );
		}
		if(newss == null ) return 0;
		return newss.size();
	}
   
	public String getNewsType(int index) {
		if(types == null ) {
			initNewsObjext( );
		}
		String type = "0"; 
		if( types == null  ) return type;
		if(index < 0 || index > types.size() -1 ) return type;
		return types.get(index);
	}
	
	public Object getNewsObject(int index) {
		Object object = null;
		if(newss == null ) {
			initNewsObjext( );
		}
		if( newss == null  ) return object;
		if(index < 0 || index > newss.size() -1 ) return object;
		return newss.get(index);
	}
	
	private void initNewsObjext( ){
		if(dataMap == null || dataMap.size() == 0 )
	    { 
			return ;
	    }
		newss = new ArrayList<Object>( );
		types = new ArrayList<String>( );
		Set<String> keys = dataMap.keySet();
		int count = 0;
		for(String key : keys) {
			count++;
			String typeStr = key.substring(key.lastIndexOf("_") +1);
			if(Util.isEmpty(typeStr) ) continue;
			String valueStr = dataMap.get(key);
			String type = "";
			Object object =null;
		//基础类型 1=单条资讯 2=多条资讯 3=音频 4=视频 5=图集 6=单本书籍 7多本书籍 , 详细类型：  video=视频 audio=音频 book=书籍
			if(typeStr.equalsIgnoreCase("imagetext")){// 单条资讯 
				type = "1";
				object = Util.jsonStringToObject(valueStr, SingleNews.class);
				newss.add(object);
				types.add(type);
			} else if(typeStr.equalsIgnoreCase("imagelist")){ //图集 
				type = "5";
				object = Util.jsonStringToObject(valueStr, ImageCollection.class);
				newss.add(object);
				types.add(type);
			}  else if(typeStr.equalsIgnoreCase("book")){//单本书籍
				type = "6";
				object = Util.jsonStringToObject(valueStr, SingleBook.class);
				newss.add(object);
				types.add(type);
			}  else if(typeStr.equalsIgnoreCase("video")){//视频
				type = "4";
				object = Util.jsonStringToObject(valueStr, AudioAndVideo.class);
				newss.add(object);
				types.add(type);
			}  else if(typeStr.equalsIgnoreCase("audio")){ //音频
				type = "3";
				object = Util.jsonStringToObject(valueStr, AudioAndVideo.class);
				newss.add(object);
				types.add(type);
			}
			if(count == 4) {
				break;
			}
		}
		//可以将dataMap释放
		dataMap = null ;
	}
	
}
